﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Telerik.Web.UI;
using SMA.GENTEv1;

public partial class Protegidos_Pessoas_DadosPessoa__popup_ppEmail : PageBase
{
    private int id = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["Id"] != null)
            id = Int32.Parse(Request.QueryString["Id"].ToString());

        if (!IsPostBack)
        {
            CarregarStatus(rcbStatus);
            CarregarTipoEmail(rcbTipoEmail);

            if (id != 0)
                CarregarPessoaEmail();
        }
    }

    #region Eventos
    protected void btnSalvar_Click(object sender, EventArgs e)
    {
        PessoaEmail email = new PessoaEmail();
        email.IdPessoaEmail = id;
        email.IdPessoa = IdPessoa;
        email.Email = txtEmail.Text;
        email.Status = new Status(Int32.Parse(rcbStatus.SelectedValue.ToString()));
        email.TipoEmail = new TipoEmail(Int32.Parse(rcbTipoEmail.SelectedValue.ToString()));

        email.Salvar();

        ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "CloseAndRebind('email');", true);
    }
    protected void btnLimpar_Click(object sender, EventArgs e)
    {
        Limpar();
    }
    #endregion

    #region Metodos
    private void CarregarTipoEmail(RadComboBox combo)
    {
        try
        {
            TipoEmail tipoEmail = new TipoEmail();

            combo.DataTextField = "Descricao";
            combo.DataValueField = "IdTipoEmail";
            combo.DataSource = tipoEmail.SelectAll();
            combo.DataBind();

            InserirSelecione(combo);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void CarregarStatus(RadComboBox combo)
    {
        try
        {
            Status status = new Status();

            combo.DataTextField = "Descricao";
            combo.DataValueField = "IdStatus";
            combo.DataSource = status.SelectAll();
            combo.DataBind();

            InserirSelecione(combo);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void InserirSelecione(RadComboBox combo)
    {
        try
        {
            combo.Items.Insert(0, new RadComboBoxItem(" -- Selecione -- ", "0"));
            combo.SelectedIndex = 0;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void CarregarPessoaEmail()
    {
        PessoaEmail pessoa = new PessoaEmail(id);
        txtEmail.Text = pessoa.Email;
        rcbTipoEmail.SelectedValue = pessoa.TipoEmail.IdTipoEmail.ToString();
        rcbStatus.SelectedValue = pessoa.Status.IdStatus.ToString();
    }
    private void Limpar()
    {
        txtEmail.Text = "";
        CarregarStatus(rcbStatus);
        CarregarTipoEmail(rcbTipoEmail);
    }
    #endregion
}
